package com.sungohealth.rules;

import com.sungohealth.bo.NumCount;
import com.sungohealth.bo.Wrapper;
import com.sungohealth.entity.Sleep;
import com.sungohealth.service.ISleepService;
import org.kie.api.KieBase;
import org.kie.api.definition.type.FactType;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.QueryResults;
import org.kie.api.runtime.rule.QueryResultsRow;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 睡眠规则
 *
 * @author :Liangzy (Potato)
 * @date: 2022-05-03 22:02
 */
@Component
public class SleepRuleOwner {

    /**
     * 规则处理器
     */
    @Resource
    private KieSession session;

    @Resource
    private KieBase kieBase;

    @Resource
    private ISleepService iSleepService;

    /**
     * 获取连续三天晚上1点之后睡，并且夜间心率大于80的人
     */
    public void toBedLate(String userId) {
        // 获取该用户的
        List<Sleep> sleepDataByUserId =
                iSleepService.getSleepDataByUserId(userId);
        sleepDataByUserId.forEach(
                x -> {
                    session.insert(x);
                }
        );

        session.fireAllRules();
    }

    /**
     * 另外一种格式
     * 获取连续三天晚上1点之后睡，并且夜间心率大于80的人
     */
    public void secondToBedLate(String userId) {
        // 获取该用户的健康数据
        List<Sleep> sleepDataByUserId =
                iSleepService.getSleepDataByUserId(userId);
        Wrapper<Sleep> wrapper = new Wrapper<>();
        wrapper.setter(sleepDataByUserId);
        session.insert(wrapper);
        session.fireAllRules();
    }

    /**
     * 睡眠状态不良
     */
    public void sleepStatusBad(String userId) {
        // 获取该用户的健康数据
        List<Sleep> sleepDataByUserId =
                iSleepService.getSleepDataByUserId(userId);
        sleepDataByUserId.forEach(
                x -> {
                    session.insert(x);
                }
        );

        session.fireAllRules();
    }

    /**
     * 平均睡眠心率超过110的天数
     */
    public void avgSleepHeartOverThis(String userId) {
        // 获取该用户的健康数据
        List<String> sleepHeartData =
                iSleepService.getSleepDataByUserId(userId)
                        .stream()
                        .map(Sleep::getHrSleepData)
                        .collect(Collectors.toList());
        sleepHeartData.forEach(
                x -> {
                    session.insert(x);
                }
        );
        session.fireAllRules();
    }

    /**
     * 计算状态为1用户的平均睡眠时长
     */
    public void avgSleepTime(String userId) {
        // 获取该用户的健康数据
        List<String> sleepHeartData =
                iSleepService.getSleepDataByUserId(userId)
                        .stream()
                        .map(Sleep::getHrSleepData)
                        .collect(Collectors.toList());
        sleepHeartData.forEach(
                x -> {
                    session.insert(x);
                }
        );
        session.fireAllRules();
    }

    /**
     * 为 working memory 中传递参数
     */
    public void declare() throws IllegalAccessException, InstantiationException {
        FactType factType = kieBase.getFactType(
                "com.sungohealth.declar",
                "Love"
        );
        Object obj = factType.newInstance();
        factType.set(obj, "feel", "happy");
        factType.set(obj, "continued", "永远");
        session.insert(obj);
        session.fireAllRules();
    }

    /**
     * 查询某条数据
     */
    public void query(String userId) {
        // 获取该用户的健康数据
        List<Sleep> sleepDataByUserId =
                iSleepService.getSleepDataByUserId(userId);
        sleepDataByUserId.forEach(
                x -> {
                    session.insert(x);
                }
        );
        QueryResults results =
                session.getQueryResults("queryPeople", "21821");
        for (QueryResultsRow row : results) {
            Sleep p = (Sleep) row.get("$user");
            System.out.println(p);
        }
    }

    /**
     * 传递公共参数
     */
    public void global(String userId) {

        Sleep sleep = new Sleep();
        sleep.setUserId(21822L);
        sleep.setStatus("1");
        session.insert(sleep);

        //配置全局变量
        List<Sleep> list = new ArrayList<>();
        NumCount numCount = new NumCount();
        session.setGlobal("list", list);
        session.setGlobal("numCount", numCount);
        session.setGlobal("service", getGlobalData(userId));
        session.fireAllRules();//执行规则
        //取出全局变量值
        System.out.println(session.getGlobal("list").toString());
        System.out.println((session.getGlobal("numCount")).toString());
    }

    /**
     * 如果睡眠时长不超过 3 小时，则设置睡眠状态为异常
     */
    public void update(String userId) {
        // 获取该用户的健康数据
        List<Sleep> sleepDataByUserId =
                iSleepService.getSleepDataByUserId(userId);
        sleepDataByUserId.forEach(
                x -> {
                    session.insert(x);
                }
        );
        session.fireAllRules();
    }

    /**
     * 获取全局数据
     */
    public List<Sleep> getGlobalData(String userId) {
        // 假装是数据源获取的数据
        return iSleepService.getSleepDataByUserId(userId)
                .stream()
                .filter(x -> x.getUserId().equals(Long.valueOf("21821")))
                .collect(Collectors.toList());
    }


    public void beforeResultExecMethod() {
        System.out.println("获取结果之后执行的方法");
    }
}
